{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "*Note: This is not yet ready, but shows the direction I'm leaning in for Fourth Edition Search.*\n",
    "\n",
    "# State-Space Search\n",
    "\n",
    "This notebook describes several state-space search algorithms, and how they can be used to solve a variety of problems. We start with a simple algorithm and a simple domain: finding a route from city to city.  Later we will explore other algorithms and domains.\n",
    "\n",
    "## The Route-Finding Domain\n",
    "\n",
    "Like all state-space search problems, in a route-finding problem you will be given:\n",
    "- A start state (for example, `'A'` for the city Arad).\n",
    "- A goal state (for example, `'B'` for the city Bucharest).\n",
    "- Actions that can change state (for example, driving from `'A'` to `'S'`).\n",
    "\n",
    "You will be asked to find:\n",
    "- A path from the start state, through intermediate states, to the goal state.\n",
    "\n",
    "We'll use this map:\n",
    "\n",
    "<img src=\"http://robotics.cs.tamu.edu/dshell/cs625/images/map.jpg\" height=\"366\" width=\"603\">\n",
    "\n",
    "A state-space search problem can be represented by a *graph*, where the vertices of the graph are the states of the problem (in this case, cities) and the edges of the graph are the actions (in this case, driving along a road).\n",
    "\n",
    "We'll represent a city by its single initial letter. \n",
    "We'll represent the graph of connections as a `dict` that maps each city to a list of the neighboring cities (connected by a road). For now we don't explicitly represent the actions, nor the distances\n",
    "between cities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "romania = {\n",
    " 'A': ['Z', 'T', 'S'],\n",
    " 'B': ['F', 'P', 'G', 'U'],\n",
    " 'C': ['D', 'R', 'P'],\n",
    " 'D': ['M', 'C'],\n",
    " 'E': ['H'],\n",
    " 'F': ['S', 'B'],\n",
    " 'G': ['B'],\n",
    " 'H': ['U', 'E'],\n",
    " 'I': ['N', 'V'],\n",
    " 'L': ['T', 'M'],\n",
    " 'M': ['L', 'D'],\n",
    " 'N': ['I'],\n",
    " 'O': ['Z', 'S'],\n",
    " 'P': ['R', 'C', 'B'],\n",
    " 'R': ['S', 'C', 'P'],\n",
    " 'S': ['A', 'O', 'F', 'R'],\n",
    " 'T': ['A', 'L'],\n",
    " 'U': ['B', 'V', 'H'],\n",
    " 'V': ['U', 'I'],\n",
    " 'Z': ['O', 'A']}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Suppose we want to get from `A` to `B`. Where can we go from the start state, `A`?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Z', 'T', 'S']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "romania['A']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "We see that from `A` we can get to any of the three cities `['Z', 'T', 'S']`. Which should we choose?  *We don't know.* That's the whole point of *search*: we don't know which immediate action is best, so we'll have to explore, until we find a *path* that leads to the goal. \n",
    "\n",
    "How do we explore? We'll start with a simple algorithm that will get us from `A` to `B`. We'll keep a *frontier*&mdash;a collection of not-yet-explored states&mdash;and expand the frontier outward until it reaches the goal. To be more precise:\n",
    "\n",
    "- Initially, the only state in the frontier is the start state, `'A'`.\n",
    "- Until we reach the goal, or run out of states in the frontier to explore, do the following:\n",
    "  - Remove the first state from the frontier. Call it `s`.\n",
    "  - If `s` is the goal, we're done. Return the path to `s`.\n",
    "  - Otherwise, consider all the neighboring states of `s`. For each one:\n",
    "    - If we have not previously explored the state, add it to the end of the frontier.\n",
    "    - Also keep track of the previous state that led to this new neighboring state; we'll need this to reconstruct the path to the goal, and to keep us from re-visiting previously explored states.\n",
    "    \n",
    "# A Simple Search Algorithm: `breadth_first`\n",
    "    \n",
    "The function `breadth_first` implements this strategy:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "from collections import deque # Doubly-ended queue: pop from left, append to right.\n",
    "\n",
    "def breadth_first(start, goal, neighbors):\n",
    "    \"Find a shortest sequence of states from start to the goal.\"\n",
    "    frontier = deque([start]) # A queue of states\n",
    "    previous = {start: None}  # start has no previous state; other states will\n",
    "    while frontier:\n",
    "        s = frontier.popleft()\n",
    "        if s == goal:\n",
    "            return path(previous, s)\n",
    "        for s2 in neighbors[s]:\n",
    "            if s2 not in previous:\n",
    "                frontier.append(s2)\n",
    "                previous[s2] = s\n",
    "                \n",
    "def path(previous, s): \n",
    "    \"Return a list of states that lead to state s, according to the previous dict.\"\n",
    "    return [] if (s is None) else path(previous, previous[s]) + [s]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "A couple of things to note: \n",
    "\n",
    "1. We always add new states to the end of the frontier queue. That means that all the states that are adjacent to the start state will come first in the queue, then all the states that are two steps away, then three steps, etc.\n",
    "That's what we mean by *breadth-first* search.\n",
    "2. We recover the path to an `end` state by following the trail of `previous[end]` pointers, all the way back to `start`.\n",
    "The dict `previous` is a map of `{state: previous_state}`. \n",
    "3. When we finally get an `s` that is the goal state, we know we have found a shortest path, because any other state in the queue must correspond to a path that is as long or longer.\n",
    "3. Note that `previous`  contains all the states that are currently in `frontier` as well as all the states that were in `frontier` in the past.\n",
    "4. If no path to the goal is found, then `breadth_first` returns `None`. If a path is found, it returns the sequence of states on the path.\n",
    "\n",
    "Some examples:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['A', 'S', 'F', 'B']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breadth_first('A', 'B', romania)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['L', 'T', 'A', 'S', 'F', 'B', 'U', 'V', 'I', 'N']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breadth_first('L', 'N', romania)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['N', 'I', 'V', 'U', 'B', 'F', 'S', 'A', 'T', 'L']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breadth_first('N', 'L', romania)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['E']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breadth_first('E', 'E', romania)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Now let's try a different  kind of problem that can be solved with the same search function.\n",
    "\n",
    "## Word Ladders Problem\n",
    "\n",
    "A *word ladder* problem is this: given a start word and a goal word, find the shortest way to transform the start word into the goal word by changing one letter at a time, such that each change results in a word. For example starting with `green` we can reach `grass` in 7 steps:\n",
    "\n",
    "`green` &rarr; `greed` &rarr; `treed` &rarr; `trees` &rarr; `tress` &rarr; `cress` &rarr; `crass` &rarr; `grass`\n",
    "\n",
    "We will need a dictionary of words. We'll use 5-letter words from the [Stanford GraphBase](http://www-cs-faculty.stanford.edu/~uno/sgb.html) project for this purpose. Let's get that file from aimadata."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "from search import *\n",
    "sgb_words = open_data(\"EN-text/sgb-words.txt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "We can assign `WORDS` to be the set of all the words in this file:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5757"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "WORDS = set(sgb_words.read().split())\n",
    "len(WORDS)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "And define `neighboring_words` to return the set of all words that are a one-letter change away from a given `word`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def neighboring_words(word):\n",
    "    \"All words that are one letter away from this word.\"\n",
    "    neighbors = {word[:i] + c + word[i+1:]\n",
    "                 for i in range(len(word))\n",
    "                 for c in 'abcdefghijklmnopqrstuvwxyz'\n",
    "                 if c != word[i]}\n",
    "    return neighbors & WORDS"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'cello', 'hallo', 'hells', 'hullo', 'jello'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neighboring_words('hello')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'would'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neighboring_words('world')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Now we can create  `word_neighbors` as a dict of `{word: {neighboring_word, ...}}`: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "word_neighbors = {word: neighboring_words(word)\n",
    "                  for word in WORDS}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "Now the `breadth_first` function can be used to solve a word ladder problem:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['green', 'greed', 'treed', 'trees', 'treys', 'trays', 'grays', 'grass']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breadth_first('green', 'grass', word_neighbors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['smart',\n",
       " 'start',\n",
       " 'stars',\n",
       " 'sears',\n",
       " 'bears',\n",
       " 'beans',\n",
       " 'brans',\n",
       " 'brand',\n",
       " 'braid',\n",
       " 'brain']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breadth_first('smart', 'brain', word_neighbors)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['frown',\n",
       " 'flown',\n",
       " 'flows',\n",
       " 'slows',\n",
       " 'slots',\n",
       " 'slits',\n",
       " 'spits',\n",
       " 'spite',\n",
       " 'smite',\n",
       " 'smile']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "breadth_first('frown', 'smile', word_neighbors)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# More General Search Algorithms\n",
    "\n",
    "Now we'll embelish the `breadth_first` algorithm to make a family of search algorithms with more capabilities:\n",
    "\n",
    "1. We distinguish between an *action* and the *result* of an action.\n",
    "3. We allow different measures of the cost of a solution (not just the number of steps in the sequence).\n",
    "4. We search through the state space in an order that is more likely to lead to an optimal solution quickly.\n",
    "\n",
    "Here's how we do these things:\n",
    "\n",
    "1. Instead of having a graph of neighboring states, we instead have an object of type *Problem*. A Problem\n",
    "has one method, `Problem.actions(state)` to return a collection of the actions that are allowed in a state,\n",
    "and another method, `Problem.result(state, action)` that says what happens when you take an action.\n",
    "2. We keep a set, `explored` of states that have already been explored. We also have a class, `Frontier`, that makes it efficient to ask if a state is on the frontier.\n",
    "3. Each action has a cost associated with it (in fact, the cost can vary with both the state and the action).\n",
    "4. The `Frontier` class acts as a priority queue, allowing the \"best\" state to be explored next.\n",
    "We represent a sequence of actions and resulting states as a linked list of `Node` objects.\n",
    "\n",
    "The algorithm `breadth_first_search` is basically the same  as `breadth_first`, but using our new conventions:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def breadth_first_search(problem):\n",
    "    \"Search for goal; paths with least number of steps first.\"\n",
    "    if problem.is_goal(problem.initial): \n",
    "        return Node(problem.initial)\n",
    "    frontier = FrontierQ(Node(problem.initial), LIFO=False)\n",
    "    explored = set()\n",
    "    while frontier:\n",
    "        node = frontier.pop()\n",
    "        explored.add(node.state)\n",
    "        for action in problem.actions(node.state):\n",
    "            child = node.child(problem, action)\n",
    "            if child.state not in explored and child.state not in frontier:\n",
    "                if problem.is_goal(child.state):\n",
    "                    return child\n",
    "                frontier.add(child)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next is `uniform_cost_search`, in which each step can have a different cost, and we still consider first one os the states with minimum cost so far."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def uniform_cost_search(problem, costfn=lambda node: node.path_cost):\n",
    "    frontier = FrontierPQ(Node(problem.initial), costfn)\n",
    "    explored = set()\n",
    "    while frontier:\n",
    "        node = frontier.pop()\n",
    "        if problem.is_goal(node.state):\n",
    "            return node\n",
    "        explored.add(node.state)\n",
    "        for action in problem.actions(node.state):\n",
    "            child = node.child(problem, action)\n",
    "            if child.state not in explored and child not in frontier:\n",
    "                frontier.add(child)\n",
    "            elif child in frontier and frontier.cost[child] < child.path_cost:\n",
    "                frontier.replace(child)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, `astar_search`  in which the cost includes an estimate of the distance to the goal as well as the distance travelled so far."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def astar_search(problem, heuristic):\n",
    "    costfn = lambda node: node.path_cost + heuristic(node.state)\n",
    "    return uniform_cost_search(problem, costfn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Search Tree Nodes\n",
    "\n",
    "The solution to a search problem is now a linked list of `Node`s, where each `Node`\n",
    "includes a `state` and the `path_cost` of getting to the state. In addition, for every `Node` except for the first (root) `Node`, there is a previous `Node` (indicating the state that lead to this `Node`) and an `action` (indicating the action taken to get here)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "class Node(object):\n",
    "    \"\"\"A node in a search tree. A search tree is spanning tree over states.\n",
    "    A Node contains a state, the previous node in the tree, the action that\n",
    "    takes us from the previous state to this state, and the path cost to get to \n",
    "    this state. If a state is arrived at by two paths, then there are two nodes \n",
    "    with the same state.\"\"\"\n",
    "\n",
    "    def __init__(self, state, previous=None, action=None, step_cost=1):\n",
    "        \"Create a search tree Node, derived from a previous Node by an action.\"\n",
    "        self.state     = state\n",
    "        self.previous  = previous\n",
    "        self.action    = action\n",
    "        self.path_cost = 0 if previous is None else (previous.path_cost + step_cost)\n",
    "\n",
    "    def __repr__(self): return \"<Node {}: {}>\".format(self.state, self.path_cost)\n",
    "    \n",
    "    def __lt__(self, other): return self.path_cost < other.path_cost\n",
    "    \n",
    "    def child(self, problem, action):\n",
    "        \"The Node you get by taking an action from this Node.\"\n",
    "        result = problem.result(self.state, action)\n",
    "        return Node(result, self, action, \n",
    "                    problem.step_cost(self.state, action, result))    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Frontiers\n",
    "\n",
    "A frontier is a collection of Nodes that acts like both a Queue and a Set. A frontier, `f`, supports these operations:\n",
    "\n",
    "* `f.add(node)`: Add a node to the Frontier.\n",
    "\n",
    "* `f.pop()`: Remove and return the \"best\" node from the frontier.\n",
    "\n",
    "* `f.replace(node)`: add this node and remove a previous node with the same state.\n",
    "\n",
    "* `state in f`: Test if some node in the frontier has arrived at state.\n",
    "\n",
    "* `f[state]`: returns the node corresponding to this state in frontier.\n",
    "\n",
    "* `len(f)`: The number of Nodes in the frontier. When the frontier is empty, `f` is *false*.\n",
    "\n",
    "We provide two kinds of frontiers: One for \"regular\" queues, either first-in-first-out (for breadth-first search) or last-in-first-out (for depth-first search), and one for priority queues, where you can specify what cost function on nodes you are trying to minimize."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "from collections import OrderedDict\n",
    "import heapq\n",
    "\n",
    "class FrontierQ(OrderedDict):\n",
    "    \"A Frontier that supports FIFO or LIFO Queue ordering.\"\n",
    "    \n",
    "    def __init__(self, initial, LIFO=False):\n",
    "        \"\"\"Initialize Frontier with an initial Node.\n",
    "        If LIFO is True, pop from the end first; otherwise from front first.\"\"\"\n",
    "        super(FrontierQ, self).__init__()\n",
    "        self.LIFO = LIFO\n",
    "        self.add(initial)\n",
    "    \n",
    "    def add(self, node):\n",
    "        \"Add a node to the frontier.\"\n",
    "        self[node.state] = node\n",
    "        \n",
    "    def pop(self):\n",
    "        \"Remove and return the next Node in the frontier.\"\n",
    "        (state, node) = self.popitem(self.LIFO)\n",
    "        return node\n",
    "    \n",
    "    def replace(self, node):\n",
    "        \"Make this node replace the nold node with the same state.\"\n",
    "        del self[node.state]\n",
    "        self.add(node)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "class FrontierPQ:\n",
    "    \"A Frontier ordered by a cost function; a Priority Queue.\"\n",
    "    \n",
    "    def __init__(self, initial, costfn=lambda node: node.path_cost):\n",
    "        \"Initialize Frontier with an initial Node, and specify a cost function.\"\n",
    "        self.heap   = []\n",
    "        self.states = {}\n",
    "        self.costfn = costfn\n",
    "        self.add(initial)\n",
    "    \n",
    "    def add(self, node):\n",
    "        \"Add node to the frontier.\"\n",
    "        cost = self.costfn(node)\n",
    "        heapq.heappush(self.heap, (cost, node))\n",
    "        self.states[node.state] = node\n",
    "        \n",
    "    def pop(self):\n",
    "        \"Remove and return the Node with minimum cost.\"\n",
    "        (cost, node) = heapq.heappop(self.heap)\n",
    "        self.states.pop(node.state, None) # remove state\n",
    "        return node\n",
    "    \n",
    "    def replace(self, node):\n",
    "        \"Make this node replace a previous node with the same state.\"\n",
    "        if node.state not in self:\n",
    "            raise ValueError('{} not there to replace'.format(node.state))\n",
    "        for (i, (cost, old_node)) in enumerate(self.heap):\n",
    "            if old_node.state == node.state:\n",
    "                self.heap[i] = (self.costfn(node), node)\n",
    "                heapq._siftdown(self.heap, 0, i)\n",
    "                return\n",
    "\n",
    "    def __contains__(self, state): return state in self.states\n",
    "    \n",
    "    def __len__(self): return len(self.heap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Search Problems\n",
    "\n",
    "`Problem` is the abstract class for all search problems. You can define your own class of problems as a subclass of `Problem`. You will need to override the `actions` and  `result` method to describe how your problem works. You will also have to either override `is_goal` or pass a collection of goal states to the initialization method.  If actions have different costs, you should override the `step_cost` method. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "class Problem(object):\n",
    "    \"\"\"The abstract class for a search problem.\"\"\"\n",
    "\n",
    "    def __init__(self, initial=None, goals=(), **additional_keywords):\n",
    "        \"\"\"Provide an initial state and optional goal states.\n",
    "        A subclass can have additional keyword arguments.\"\"\"\n",
    "        self.initial = initial  # The initial state of the problem.\n",
    "        self.goals = goals      # A collection of possible goal states.\n",
    "        self.__dict__.update(**additional_keywords)\n",
    "\n",
    "    def actions(self, state):\n",
    "        \"Return a list of actions executable in this state.\"\n",
    "        raise NotImplementedError # Override this!\n",
    "\n",
    "    def result(self, state, action):\n",
    "        \"The state that results from executing this action in this state.\"\n",
    "        raise NotImplementedError # Override this!\n",
    "\n",
    "    def is_goal(self, state):\n",
    "        \"True if the state is a goal.\" \n",
    "        return state in self.goals # Optionally override this!\n",
    "\n",
    "    def step_cost(self, state, action, result=None):\n",
    "        \"The cost of taking this action from this state.\"\n",
    "        return 1 # Override this if actions have different costs        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def action_sequence(node):\n",
    "    \"The sequence of actions to get to this node.\"\n",
    "    actions = []\n",
    "    while node.previous:\n",
    "        actions.append(node.action)\n",
    "        node = node.previous\n",
    "    return actions[::-1]\n",
    "\n",
    "def state_sequence(node):\n",
    "    \"The sequence of states to get to this node.\"\n",
    "    states = [node.state]\n",
    "    while node.previous:\n",
    "        node = node.previous\n",
    "        states.append(node.state)\n",
    "    return states[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Two Location Vacuum World"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "dirt  = '*'\n",
    "clean = ' '\n",
    "\n",
    "class TwoLocationVacuumProblem(Problem):\n",
    "    \"\"\"A Vacuum in a world with two locations, and dirt.\n",
    "    Each state is a tuple of (location, dirt_in_W, dirt_in_E).\"\"\"\n",
    "\n",
    "    def actions(self, state): return ('W', 'E', 'Suck')\n",
    "    \n",
    "    def is_goal(self, state): return dirt not in state\n",
    " \n",
    "    def result(self, state, action):\n",
    "        \"The state that results from executing this action in this state.\"        \n",
    "        (loc, dirtW, dirtE) = state\n",
    "        if   action == 'W':                   return ('W', dirtW, dirtE)\n",
    "        elif action == 'E':                   return ('E', dirtW, dirtE)\n",
    "        elif action == 'Suck' and loc == 'W': return (loc, clean, dirtE)\n",
    "        elif action == 'Suck' and loc == 'E': return (loc, dirtW, clean) \n",
    "        else: raise ValueError('unknown action: ' + action)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Node ('E', ' ', ' '): 3>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "problem = TwoLocationVacuumProblem(initial=('W', dirt, dirt))\n",
    "result = uniform_cost_search(problem)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Suck', 'E', 'Suck']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "action_sequence(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('W', '*', '*'), ('W', ' ', '*'), ('E', ' ', '*'), ('E', ' ', ' ')]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "state_sequence(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Suck']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "problem = TwoLocationVacuumProblem(initial=('E', clean, dirt))\n",
    "result = uniform_cost_search(problem)\n",
    "action_sequence(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Water Pouring Problem\n",
    "\n",
    "Here is another problem domain, to show you how to define one. The idea is that we have a number of water jugs and a water tap and the goal is to measure out a specific amount of water (in, say, ounces or liters). You can completely fill or empty a jug, but because the jugs don't have markings on them, you can't partially fill them with a specific amount. You can, however, pour one jug into another, stopping when the seconfd is full or the first is empty."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "class PourProblem(Problem):\n",
    "    \"\"\"Problem about pouring water between jugs to achieve some water level.\n",
    "    Each state is a tuples of levels. In the initialization, provide a tuple of \n",
    "    capacities, e.g. PourProblem(capacities=(8, 16, 32), initial=(2, 4, 3), goals={7}), \n",
    "    which means three jugs of capacity 8, 16, 32, currently filled with 2, 4, 3 units of \n",
    "    water, respectively, and the goal is to get a level of 7 in any one of the jugs.\"\"\"\n",
    "    \n",
    "    def actions(self, state):\n",
    "        \"\"\"The actions executable in this state.\"\"\"\n",
    "        jugs = range(len(state))\n",
    "        return ([('Fill', i)    for i in jugs if state[i] != self.capacities[i]] +\n",
    "                [('Dump', i)    for i in jugs if state[i] != 0] +\n",
    "                [('Pour', i, j) for i in jugs for j in jugs if i != j])\n",
    "\n",
    "    def result(self, state, action):\n",
    "        \"\"\"The state that results from executing this action in this state.\"\"\"\n",
    "        result = list(state)\n",
    "        act, i, j = action[0], action[1], action[-1]\n",
    "        if act == 'Fill': # Fill i to capacity\n",
    "            result[i] = self.capacities[i]\n",
    "        elif act == 'Dump': # Empty i\n",
    "            result[i] = 0\n",
    "        elif act == 'Pour':\n",
    "            a, b = state[i], state[j]\n",
    "            result[i], result[j] = ((0, a + b) \n",
    "                                    if (a + b <= self.capacities[j]) else\n",
    "                                    (a + b - self.capacities[j], self.capacities[j]))\n",
    "        else:\n",
    "            raise ValueError('unknown action', action)\n",
    "        return tuple(result)\n",
    "\n",
    "    def is_goal(self, state):\n",
    "        \"\"\"True if any of the jugs has a level equal to one of the goal levels.\"\"\"\n",
    "        return any(level in self.goals for level in state)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 13)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p7 = PourProblem(initial=(2, 0), capacities=(5, 13), goals={7})\n",
    "p7.result((2, 0),  ('Fill', 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Pour', 0, 1), ('Fill', 0), ('Pour', 0, 1)]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = uniform_cost_search(p7)\n",
    "action_sequence(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Visualization Output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def showpath(searcher, problem):\n",
    "    \"Show what happens when searcvher solves problem.\"\n",
    "    problem = Instrumented(problem)\n",
    "    print('\\n{}:'.format(searcher.__name__))\n",
    "    result = searcher(problem)\n",
    "    if result:\n",
    "        actions = action_sequence(result)\n",
    "        state = problem.initial\n",
    "        path_cost = 0\n",
    "        for steps, action in enumerate(actions, 1):\n",
    "            path_cost += problem.step_cost(state, action, 0)\n",
    "            result = problem.result(state, action)\n",
    "            print('  {} =={}==> {}; cost {} after {} steps'\n",
    "                  .format(state, action, result, path_cost, steps,\n",
    "                          '; GOAL!' if problem.is_goal(result) else ''))\n",
    "            state = result\n",
    "    msg = 'GOAL FOUND' if result else 'no solution'\n",
    "    print('{} after {} results and {} goal checks'\n",
    "          .format(msg, problem._counter['result'], problem._counter['is_goal']))\n",
    "        \n",
    "from collections import Counter\n",
    "\n",
    "class Instrumented:\n",
    "    \"Instrument an object to count all the attribute accesses in _counter.\"\n",
    "    def __init__(self, obj):\n",
    "        self._object = obj\n",
    "        self._counter = Counter()\n",
    "    def __getattr__(self, attr):\n",
    "        self._counter[attr] += 1\n",
    "        return getattr(self._object, attr)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "uniform_cost_search:\n",
      "  (2, 0) ==('Pour', 0, 1)==> (0, 2); cost 1 after 1 steps\n",
      "  (0, 2) ==('Fill', 0)==> (5, 2); cost 2 after 2 steps\n",
      "  (5, 2) ==('Pour', 0, 1)==> (0, 7); cost 3 after 3 steps\n",
      "GOAL FOUND after 83 results and 22 goal checks\n"
     ]
    }
   ],
   "source": [
    "showpath(uniform_cost_search, p7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "uniform_cost_search:\n",
      "  (0, 0) ==('Fill', 0)==> (7, 0); cost 1 after 1 steps\n",
      "  (7, 0) ==('Pour', 0, 1)==> (0, 7); cost 2 after 2 steps\n",
      "  (0, 7) ==('Fill', 0)==> (7, 7); cost 3 after 3 steps\n",
      "  (7, 7) ==('Pour', 0, 1)==> (1, 13); cost 4 after 4 steps\n",
      "  (1, 13) ==('Dump', 1)==> (1, 0); cost 5 after 5 steps\n",
      "  (1, 0) ==('Pour', 0, 1)==> (0, 1); cost 6 after 6 steps\n",
      "  (0, 1) ==('Fill', 0)==> (7, 1); cost 7 after 7 steps\n",
      "  (7, 1) ==('Pour', 0, 1)==> (0, 8); cost 8 after 8 steps\n",
      "  (0, 8) ==('Fill', 0)==> (7, 8); cost 9 after 9 steps\n",
      "  (7, 8) ==('Pour', 0, 1)==> (2, 13); cost 10 after 10 steps\n",
      "GOAL FOUND after 110 results and 32 goal checks\n"
     ]
    }
   ],
   "source": [
    "p = PourProblem(initial=(0, 0), capacities=(7, 13), goals={2})\n",
    "showpath(uniform_cost_search, p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class GreenPourProblem(PourProblem):    \n",
    "    def step_cost(self, state, action, result=None):\n",
    "        \"The cost is the amount of water used in a fill.\"\n",
    "        if action[0] == 'Fill':\n",
    "            i = action[1]\n",
    "            return self.capacities[i] - state[i]\n",
    "        return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "uniform_cost_search:\n",
      "  (0, 0) ==('Fill', 0)==> (7, 0); cost 7 after 1 steps\n",
      "  (7, 0) ==('Pour', 0, 1)==> (0, 7); cost 7 after 2 steps\n",
      "  (0, 7) ==('Fill', 0)==> (7, 7); cost 14 after 3 steps\n",
      "  (7, 7) ==('Pour', 0, 1)==> (1, 13); cost 14 after 4 steps\n",
      "  (1, 13) ==('Dump', 1)==> (1, 0); cost 14 after 5 steps\n",
      "  (1, 0) ==('Pour', 0, 1)==> (0, 1); cost 14 after 6 steps\n",
      "  (0, 1) ==('Fill', 0)==> (7, 1); cost 21 after 7 steps\n",
      "  (7, 1) ==('Pour', 0, 1)==> (0, 8); cost 21 after 8 steps\n",
      "  (0, 8) ==('Fill', 0)==> (7, 8); cost 28 after 9 steps\n",
      "  (7, 8) ==('Pour', 0, 1)==> (2, 13); cost 28 after 10 steps\n",
      "GOAL FOUND after 184 results and 48 goal checks\n"
     ]
    }
   ],
   "source": [
    "p = GreenPourProblem(initial=(0, 0), capacities=(7, 13), goals={2})\n",
    "showpath(uniform_cost_search, p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def compare_searchers(problem, searchers=None):\n",
    "    \"Apply each of the search algorithms to the problem, and show results\"\n",
    "    if searchers is None: \n",
    "        searchers = (breadth_first_search, uniform_cost_search)\n",
    "    for searcher in searchers:\n",
    "        showpath(searcher, problem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "breadth_first_search:\n",
      "  (0, 0) ==('Fill', 0)==> (7, 0); cost 7 after 1 steps\n",
      "  (7, 0) ==('Pour', 0, 1)==> (0, 7); cost 7 after 2 steps\n",
      "  (0, 7) ==('Fill', 0)==> (7, 7); cost 14 after 3 steps\n",
      "  (7, 7) ==('Pour', 0, 1)==> (1, 13); cost 14 after 4 steps\n",
      "  (1, 13) ==('Dump', 1)==> (1, 0); cost 14 after 5 steps\n",
      "  (1, 0) ==('Pour', 0, 1)==> (0, 1); cost 14 after 6 steps\n",
      "  (0, 1) ==('Fill', 0)==> (7, 1); cost 21 after 7 steps\n",
      "  (7, 1) ==('Pour', 0, 1)==> (0, 8); cost 21 after 8 steps\n",
      "  (0, 8) ==('Fill', 0)==> (7, 8); cost 28 after 9 steps\n",
      "  (7, 8) ==('Pour', 0, 1)==> (2, 13); cost 28 after 10 steps\n",
      "GOAL FOUND after 100 results and 31 goal checks\n",
      "\n",
      "uniform_cost_search:\n",
      "  (0, 0) ==('Fill', 0)==> (7, 0); cost 7 after 1 steps\n",
      "  (7, 0) ==('Pour', 0, 1)==> (0, 7); cost 7 after 2 steps\n",
      "  (0, 7) ==('Fill', 0)==> (7, 7); cost 14 after 3 steps\n",
      "  (7, 7) ==('Pour', 0, 1)==> (1, 13); cost 14 after 4 steps\n",
      "  (1, 13) ==('Dump', 1)==> (1, 0); cost 14 after 5 steps\n",
      "  (1, 0) ==('Pour', 0, 1)==> (0, 1); cost 14 after 6 steps\n",
      "  (0, 1) ==('Fill', 0)==> (7, 1); cost 21 after 7 steps\n",
      "  (7, 1) ==('Pour', 0, 1)==> (0, 8); cost 21 after 8 steps\n",
      "  (0, 8) ==('Fill', 0)==> (7, 8); cost 28 after 9 steps\n",
      "  (7, 8) ==('Pour', 0, 1)==> (2, 13); cost 28 after 10 steps\n",
      "GOAL FOUND after 184 results and 48 goal checks\n"
     ]
    }
   ],
   "source": [
    "compare_searchers(p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Random Grid\n",
    "\n",
    "An environment where you can move in any of 4 directions, unless there is an obstacle there.\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{(0, 0): [(0, 1), (1, 0)],\n",
       " (0, 1): [(0, 2), (0, 0), (1, 1)],\n",
       " (0, 2): [(0, 3), (0, 1), (1, 2)],\n",
       " (0, 3): [(0, 4), (0, 2), (1, 3)],\n",
       " (0, 4): [(0, 3), (1, 4)],\n",
       " (1, 0): [(1, 1), (2, 0), (0, 0)],\n",
       " (1, 1): [(1, 2), (1, 0), (2, 1), (0, 1)],\n",
       " (1, 2): [(1, 3), (1, 1), (2, 2), (0, 2)],\n",
       " (1, 3): [(1, 4), (1, 2), (2, 3), (0, 3)],\n",
       " (1, 4): [(1, 3), (2, 4), (0, 4)],\n",
       " (2, 0): [(2, 1), (3, 0), (1, 0)],\n",
       " (2, 1): [(2, 2), (2, 0), (3, 1), (1, 1)],\n",
       " (2, 2): [(2, 3), (2, 1), (1, 2)],\n",
       " (2, 3): [(2, 4), (2, 2), (3, 3), (1, 3)],\n",
       " (2, 4): [(2, 3), (1, 4)],\n",
       " (3, 0): [(3, 1), (4, 0), (2, 0)],\n",
       " (3, 1): [(3, 0), (4, 1), (2, 1)],\n",
       " (3, 2): [(3, 3), (3, 1), (4, 2), (2, 2)],\n",
       " (3, 3): [(4, 3), (2, 3)],\n",
       " (3, 4): [(3, 3), (4, 4), (2, 4)],\n",
       " (4, 0): [(4, 1), (3, 0)],\n",
       " (4, 1): [(4, 2), (4, 0), (3, 1)],\n",
       " (4, 2): [(4, 3), (4, 1)],\n",
       " (4, 3): [(4, 4), (4, 2), (3, 3)],\n",
       " (4, 4): [(4, 3)]}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "\n",
    "N, S, E, W = DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0)]\n",
    "\n",
    "def Grid(width, height, obstacles=0.1):\n",
    "    \"\"\"A 2-D grid, width x height, with obstacles that are either a collection of points,\n",
    "    or a fraction between 0 and 1 indicating the density of obstacles, chosen at random.\"\"\"\n",
    "    grid = {(x, y) for x in range(width) for y in range(height)}\n",
    "    if isinstance(obstacles, (float, int)):\n",
    "        obstacles = random.sample(grid, int(width * height * obstacles))\n",
    "    def neighbors(x, y):\n",
    "        for (dx, dy) in DIRECTIONS:\n",
    "            (nx, ny) = (x + dx, y + dy)\n",
    "            if (nx, ny) not in obstacles and 0 <= nx < width and 0 <= ny < height:\n",
    "                yield (nx, ny)\n",
    "    return {(x, y): list(neighbors(x, y))\n",
    "            for x in range(width) for y in range(height)}\n",
    "\n",
    "Grid(5, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class GridProblem(Problem):\n",
    "    \"Create with a call like GridProblem(grid=Grid(10, 10), initial=(0, 0), goal=(9, 9))\"\n",
    "    def actions(self, state): return DIRECTIONS\n",
    "    def result(self, state, action):\n",
    "        #print('ask for result of', state, action)\n",
    "        (x, y) = state\n",
    "        (dx, dy) = action\n",
    "        r = (x + dx, y + dy)\n",
    "        return r if r in self.grid[state] else state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "uniform_cost_search:\n",
      "  (0, 0) ==(0, 1)==> (0, 1); cost 1 after 1 steps\n",
      "  (0, 1) ==(0, 1)==> (0, 2); cost 2 after 2 steps\n",
      "  (0, 2) ==(0, 1)==> (0, 3); cost 3 after 3 steps\n",
      "  (0, 3) ==(1, 0)==> (1, 3); cost 4 after 4 steps\n",
      "  (1, 3) ==(1, 0)==> (2, 3); cost 5 after 5 steps\n",
      "  (2, 3) ==(0, 1)==> (2, 4); cost 6 after 6 steps\n",
      "  (2, 4) ==(1, 0)==> (3, 4); cost 7 after 7 steps\n",
      "  (3, 4) ==(1, 0)==> (4, 4); cost 8 after 8 steps\n",
      "GOAL FOUND after 248 results and 69 goal checks\n"
     ]
    }
   ],
   "source": [
    "gp = GridProblem(grid=Grid(5, 5, 0.3), initial=(0, 0), goals={(4, 4)})\n",
    "showpath(uniform_cost_search, gp)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "source": [
    "# Finding a hard PourProblem\n",
    "\n",
    "What solvable two-jug PourProblem requires the most steps? We can define the hardness as the number of steps, and then iterate over all PourProblems with capacities up to size M, keeping the hardest one."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "def hardness(problem):\n",
    "    L = breadth_first_search(problem)\n",
    "    #print('hardness', problem.initial, problem.capacities, problem.goals, L)\n",
    "    return len(action_sequence(L)) if (L is not None) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hardness(p7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Pour', 0, 1), ('Fill', 0), ('Pour', 0, 1)]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "action_sequence(breadth_first_search(p7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((0, 0), (7, 9), {8})"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "C = 9 # Maximum capacity to consider\n",
    "\n",
    "phard = max((PourProblem(initial=(a, b), capacities=(A, B), goals={goal})\n",
    "             for A in range(C+1) for B in range(C+1)\n",
    "             for a in range(A) for b in range(B)\n",
    "             for goal in range(max(A, B))),\n",
    "            key=hardness)\n",
    "\n",
    "phard.initial, phard.capacities, phard.goals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "breadth_first_search:\n",
      "  (0, 0) ==('Fill', 1)==> (0, 9); cost 1 after 1 steps\n",
      "  (0, 9) ==('Pour', 1, 0)==> (7, 2); cost 2 after 2 steps\n",
      "  (7, 2) ==('Dump', 0)==> (0, 2); cost 3 after 3 steps\n",
      "  (0, 2) ==('Pour', 1, 0)==> (2, 0); cost 4 after 4 steps\n",
      "  (2, 0) ==('Fill', 1)==> (2, 9); cost 5 after 5 steps\n",
      "  (2, 9) ==('Pour', 1, 0)==> (7, 4); cost 6 after 6 steps\n",
      "  (7, 4) ==('Dump', 0)==> (0, 4); cost 7 after 7 steps\n",
      "  (0, 4) ==('Pour', 1, 0)==> (4, 0); cost 8 after 8 steps\n",
      "  (4, 0) ==('Fill', 1)==> (4, 9); cost 9 after 9 steps\n",
      "  (4, 9) ==('Pour', 1, 0)==> (7, 6); cost 10 after 10 steps\n",
      "  (7, 6) ==('Dump', 0)==> (0, 6); cost 11 after 11 steps\n",
      "  (0, 6) ==('Pour', 1, 0)==> (6, 0); cost 12 after 12 steps\n",
      "  (6, 0) ==('Fill', 1)==> (6, 9); cost 13 after 13 steps\n",
      "  (6, 9) ==('Pour', 1, 0)==> (7, 8); cost 14 after 14 steps\n",
      "GOAL FOUND after 150 results and 44 goal checks\n"
     ]
    }
   ],
   "source": [
    "showpath(breadth_first_search, PourProblem(initial=(0, 0), capacities=(7, 9), goals={8}))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "uniform_cost_search:\n",
      "  (0, 0) ==('Fill', 1)==> (0, 9); cost 1 after 1 steps\n",
      "  (0, 9) ==('Pour', 1, 0)==> (7, 2); cost 2 after 2 steps\n",
      "  (7, 2) ==('Dump', 0)==> (0, 2); cost 3 after 3 steps\n",
      "  (0, 2) ==('Pour', 1, 0)==> (2, 0); cost 4 after 4 steps\n",
      "  (2, 0) ==('Fill', 1)==> (2, 9); cost 5 after 5 steps\n",
      "  (2, 9) ==('Pour', 1, 0)==> (7, 4); cost 6 after 6 steps\n",
      "  (7, 4) ==('Dump', 0)==> (0, 4); cost 7 after 7 steps\n",
      "  (0, 4) ==('Pour', 1, 0)==> (4, 0); cost 8 after 8 steps\n",
      "  (4, 0) ==('Fill', 1)==> (4, 9); cost 9 after 9 steps\n",
      "  (4, 9) ==('Pour', 1, 0)==> (7, 6); cost 10 after 10 steps\n",
      "  (7, 6) ==('Dump', 0)==> (0, 6); cost 11 after 11 steps\n",
      "  (0, 6) ==('Pour', 1, 0)==> (6, 0); cost 12 after 12 steps\n",
      "  (6, 0) ==('Fill', 1)==> (6, 9); cost 13 after 13 steps\n",
      "  (6, 9) ==('Pour', 1, 0)==> (7, 8); cost 14 after 14 steps\n",
      "GOAL FOUND after 159 results and 45 goal checks\n"
     ]
    }
   ],
   "source": [
    "showpath(uniform_cost_search, phard)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "class GridProblem(Problem):\n",
    "    \"\"\"A Grid.\"\"\"\n",
    "\n",
    "    def actions(self, state): return ['N', 'S', 'E', 'W']        \n",
    " \n",
    "    def result(self, state, action):\n",
    "        \"\"\"The state that results from executing this action in this state.\"\"\"  \n",
    "        (W, H) = self.size\n",
    "        if action == 'N' and state > W:           return state - W\n",
    "        if action == 'S' and state + W < W * W:   return state + W\n",
    "        if action == 'E' and (state + 1) % W !=0: return state + 1\n",
    "        if action == 'W' and state % W != 0:      return state - 1\n",
    "        return state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "breadth_first_search:\n",
      "  0 ==S==> 10; cost 1 after 1 steps\n",
      "  10 ==S==> 20; cost 2 after 2 steps\n",
      "  20 ==S==> 30; cost 3 after 3 steps\n",
      "  30 ==S==> 40; cost 4 after 4 steps\n",
      "  40 ==E==> 41; cost 5 after 5 steps\n",
      "  41 ==E==> 42; cost 6 after 6 steps\n",
      "  42 ==E==> 43; cost 7 after 7 steps\n",
      "  43 ==E==> 44; cost 8 after 8 steps\n",
      "GOAL FOUND after 135 results and 49 goal checks\n",
      "\n",
      "uniform_cost_search:\n",
      "  0 ==S==> 10; cost 1 after 1 steps\n",
      "  10 ==S==> 20; cost 2 after 2 steps\n",
      "  20 ==E==> 21; cost 3 after 3 steps\n",
      "  21 ==E==> 22; cost 4 after 4 steps\n",
      "  22 ==E==> 23; cost 5 after 5 steps\n",
      "  23 ==S==> 33; cost 6 after 6 steps\n",
      "  33 ==S==> 43; cost 7 after 7 steps\n",
      "  43 ==E==> 44; cost 8 after 8 steps\n",
      "GOAL FOUND after 1036 results and 266 goal checks\n"
     ]
    }
   ],
   "source": [
    "compare_searchers(GridProblem(initial=0, goals={44}, size=(10, 10)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'test_frontier ok'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def test_frontier():\n",
    "    \n",
    "    #### Breadth-first search with FIFO Q\n",
    "    f = FrontierQ(Node(1), LIFO=False)\n",
    "    assert 1 in f and len(f) == 1\n",
    "    f.add(Node(2))\n",
    "    f.add(Node(3))\n",
    "    assert 1 in f and 2 in f and 3 in f and len(f) == 3\n",
    "    assert f.pop().state == 1\n",
    "    assert 1 not in f and 2 in f and 3 in f and len(f) == 2\n",
    "    assert f\n",
    "    assert f.pop().state == 2\n",
    "    assert f.pop().state == 3\n",
    "    assert not f\n",
    "    \n",
    "    #### Depth-first search with LIFO Q\n",
    "    f = FrontierQ(Node('a'), LIFO=True)\n",
    "    for s in 'bcdef': f.add(Node(s))\n",
    "    assert len(f) == 6 and 'a' in f and 'c' in f and 'f' in f\n",
    "    for s in 'fedcba': assert f.pop().state == s\n",
    "    assert not f\n",
    "\n",
    "    #### Best-first search with Priority Q\n",
    "    f = FrontierPQ(Node(''), lambda node: len(node.state))\n",
    "    assert '' in f and len(f) == 1 and f\n",
    "    for s in ['book', 'boo', 'bookie', 'bookies', 'cook', 'look', 'b']:\n",
    "        assert s not in f\n",
    "        f.add(Node(s))\n",
    "        assert s in f\n",
    "    assert f.pop().state == ''\n",
    "    assert f.pop().state == 'b'\n",
    "    assert f.pop().state == 'boo'\n",
    "    assert {f.pop().state for _ in '123'} == {'book', 'cook', 'look'}\n",
    "    assert f.pop().state == 'bookie'\n",
    "    \n",
    "    #### Romania: Two paths to Bucharest; cheapest one found first\n",
    "    S    = Node('S')\n",
    "    SF   = Node('F', S, 'S->F', 99)\n",
    "    SFB  = Node('B', SF, 'F->B', 211)\n",
    "    SR   = Node('R', S, 'S->R', 80)\n",
    "    SRP  = Node('P', SR, 'R->P', 97)\n",
    "    SRPB = Node('B', SRP, 'P->B', 101)\n",
    "    f = FrontierPQ(S)\n",
    "    f.add(SF); f.add(SR), f.add(SRP), f.add(SRPB); f.add(SFB)\n",
    "    def cs(n): return (n.path_cost, n.state) # cs: cost and state\n",
    "    assert cs(f.pop()) == (0, 'S')\n",
    "    assert cs(f.pop()) == (80, 'R')\n",
    "    assert cs(f.pop()) == (99, 'F')\n",
    "    assert cs(f.pop()) == (177, 'P')\n",
    "    assert cs(f.pop()) == (278, 'B')\n",
    "    return 'test_frontier ok'\n",
    "\n",
    "test_frontier()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "button": false,
    "collapsed": true,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [],
   "source": [
    "# %matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "p = plt.plot([i**2 for i in range(10)])\n",
    "plt.savefig('destination_path.eps', format='eps', dpi=1200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "button": false,
    "new_sheet": false,
    "run_control": {
     "read_only": false
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0K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bi1F9zJjAD\nyDKzdb5t9/vWdhUBuA1Y7Dv42QXc4HEeTzjnVpvZMmAtdWeHZdIKrhjVlaIiImEiFIZcRETEDyp0\nEZEwoUIXEQkTKnQRkTChQhcRCRMqdBGRMKFCFxEJEyp0EZEw8f/pavD4X6i2SQAAAABJRU5ErkJg\ngg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f876647a860>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeYAAAHSCAYAAAA5eGh0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAIABJREFUeJzt219Mk/f///9HaxOnNULI1iIWxrSi\nk8URHfFPYjK7g2YaiOhUkkUPdrIlLNmOfX8mduLigYdkMYsHYKIBBDM2o43GGNEjdIZEl2iE6JQ/\nFlw0bI1b4aLfA3/29674nuCAvvrifjuif676fPC6ruvRq0VXMpkUAAAwgzvTAwAAgP8fxQwAgEEo\nZgAADEIxAwBgEIoZAACDUMwAABjEk+kBXseBAwcejo2N+TM9x1Rzu91jY2Nj1r1ZSiaTYy6Xy7pc\nkr1r5vF4xkZHR63LJdm7P86ZM2fMcRzrctl6jEmS2+2OffPNN/kv3p+VxTw2Nubfvn17pseYcm1t\nbW5bc8VisUyPMS38fr+1a1ZbW5vpMaZFJBKxcn/0+/1WrlkkErHyGJOktra2l15gWvkuBACAbEUx\nAwBgEIoZAACDUMwAABiEYgYAwCAUMwAABqGYAQAwCMUMAIBBKGYAAAxCMQMAYBCKGQAAg1DMAAAY\nhGIGAMAgFDMAAAahmAEAMAjFDACAQShmAAAMQjEDAGAQihkAAINQzAAAGGTWFvOVK1dUUVGhzZs3\n6+jRo+Mev3btmnbu3KmysjKdO3cudf+tW7f06a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z5ow5jmNdLsneNXO73WNjY2PW5ZIkj8czNjo6al02W/dFm88fHo8n9p//\n/Cf/xfuzspgBALCVle9CAADIVhQzAAAGoZgBADAIxQwAgEEoZgAADEIxAwBgEIoZAACDUMwAABiE\nYgYAwCAUMwAABqGYAQAwCMUMAIBBKGYAAAxCMQMAYBCKGQAAg1DMAAAYhGIGAMAgFDMAAAahmAEA\nMAjFDACAQShmAAAMQjEDAGCQ/wdJuZEoaHGMKwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x7f874e648390>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import itertools\n",
    "import random\n",
    "# http://stackoverflow.com/questions/10194482/custom-matplotlib-plot-chess-board-like-table-with-colored-cells\n",
    "\n",
    "from matplotlib.table import Table\n",
    "\n",
    "def main():\n",
    "    grid_table(8, 8)\n",
    "    plt.axis('scaled')\n",
    "    plt.show()\n",
    "\n",
    "def grid_table(nrows, ncols):\n",
    "    fig, ax = plt.subplots()\n",
    "    ax.set_axis_off()\n",
    "    colors = ['white', 'lightgrey', 'dimgrey']\n",
    "    tb = Table(ax, bbox=[0,0,2,2])\n",
    "    for i,j in itertools.product(range(ncols), range(nrows)):\n",
    "        tb.add_cell(i, j, 2./ncols, 2./nrows, text='{:0.2f}'.format(0.1234), \n",
    "                    loc='center', facecolor=random.choice(colors), edgecolor='grey') # facecolors=\n",
    "    ax.add_table(tb)\n",
    "    #ax.plot([0, .3], [.2, .2])\n",
    "    #ax.add_line(plt.Line2D([0.3, 0.5], [0.7, 0.7], linewidth=2, color='blue'))\n",
    "    return fig\n",
    "\n",
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import collections\n",
    "class defaultkeydict(collections.defaultdict):\n",
    "    \"\"\"Like defaultdict, but the default_factory is a function of the key.\n",
    "    >>> d = defaultkeydict(abs); d[-42]\n",
    "    42\n",
    "    \"\"\"\n",
    "    def __missing__(self, key):\n",
    "        self[key] = self.default_factory(key)\n",
    "        return self[key]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Simulated Annealing visualisation using TSP\n",
    "\n",
    "Applying simulated annealing in traveling salesman problem to find the shortest tour to travel all cities in Romania. Distance between two cities is taken as the euclidean distance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class TSP_problem(Problem):\n",
    "\n",
    "    '''\n",
    "    subclass of Problem to define various functions \n",
    "    '''\n",
    "\n",
    "    def two_opt(self, state):\n",
    "        '''\n",
    "        Neighbour generating function for Traveling Salesman Problem\n",
    "        '''\n",
    "        state2 = state[:]\n",
    "        l = random.randint(0, len(state2) - 1)\n",
    "        r = random.randint(0, len(state2) - 1)\n",
    "        if l > r:\n",
    "            l, r = r,l\n",
    "        state2[l : r + 1] = reversed(state2[l : r + 1])\n",
    "        return state2\n",
    "\n",
    "    def actions(self, state):\n",
    "        '''\n",
    "        action that can be excuted in given state\n",
    "        '''\n",
    "        return [self.two_opt]\n",
    "    \n",
    "    def result(self, state, action):\n",
    "        '''\n",
    "        result after applying the given action on the given state\n",
    "        '''\n",
    "        return action(state)\n",
    "\n",
    "    def path_cost(self, c, state1, action, state2):\n",
    "        '''\n",
    "        total distance for the Traveling Salesman to be covered if in state2\n",
    "        '''\n",
    "        cost = 0\n",
    "        for i in range(len(state2) - 1):\n",
    "            cost += distances[state2[i]][state2[i + 1]]\n",
    "        cost += distances[state2[0]][state2[-1]]\n",
    "        return cost\n",
    " \n",
    "    def value(self, state):\n",
    "        '''\n",
    "        value of path cost given negative for the given state\n",
    "        '''\n",
    "        return -1 * self.path_cost(None, None, None, state)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def init():\n",
    "    ''' \n",
    "    Initialisation function for matplotlib animation\n",
    "    '''\n",
    "    line.set_data([], [])\n",
    "    for name, coordinates in romania_map.locations.items():\n",
    "            ax.annotate(\n",
    "            name,\n",
    "            xy=coordinates, xytext=(-10, 5), textcoords='offset points', size = 10)\n",
    "    text.set_text(\"Cost = 0 i = 0\" )\n",
    "\n",
    "    return line, \n",
    "\n",
    "def animate(i):\n",
    "    '''\n",
    "    Animation function to set next path and print its cost.\n",
    "    '''\n",
    "    x, y = [], []\n",
    "    for name in states[i]:\n",
    "        x.append(romania_map.locations[name][0])\n",
    "        y.append(romania_map.locations[name][1])\n",
    "    x.append(romania_map.locations[states[i][0]][0])\n",
    "    y.append(romania_map.locations[states[i][0]][1])\n",
    "    line.set_data(x,y) \n",
    "    text.set_text(\"Cost = \" + str('{:.2f}'.format(TSP_problem.path_cost(None, None, None, None, states[i]))))\n",
    "    return line,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "/* Put everything inside the global mpl namespace */\n",
       "window.mpl = {};\n",
       "\n",
       "\n",
       "mpl.get_websocket_type = function() {\n",
       "    if (typeof(WebSocket) !== 'undefined') {\n",
       "        return WebSocket;\n",
       "    } else if (typeof(MozWebSocket) !== 'undefined') {\n",
       "        return MozWebSocket;\n",
       "    } else {\n",
       "        alert('Your browser does not have WebSocket support.' +\n",
       "              'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
       "              'Firefox 4 and 5 are also supported but you ' +\n",
       "              'have to enable WebSockets in about:config.');\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
       "    this.id = figure_id;\n",
       "\n",
       "    this.ws = websocket;\n",
       "\n",
       "    this.supports_binary = (this.ws.binaryType != undefined);\n",
       "\n",
       "    if (!this.supports_binary) {\n",
       "        var warnings = document.getElementById(\"mpl-warnings\");\n",
       "        if (warnings) {\n",
       "            warnings.style.display = 'block';\n",
       "            warnings.textContent = (\n",
       "                \"This browser does not support binary websocket messages. \" +\n",
       "                    \"Performance may be slow.\");\n",
       "        }\n",
       "    }\n",
       "\n",
       "    this.imageObj = new Image();\n",
       "\n",
       "    this.context = undefined;\n",
       "    this.message = undefined;\n",
       "    this.canvas = undefined;\n",
       "    this.rubberband_canvas = undefined;\n",
       "    this.rubberband_context = undefined;\n",
       "    this.format_dropdown = undefined;\n",
       "\n",
       "    this.image_mode = 'full';\n",
       "\n",
       "    this.root = $('<div/>');\n",
       "    this._root_extra_style(this.root)\n",
       "    this.root.attr('style', 'display: inline-block');\n",
       "\n",
       "    $(parent_element).append(this.root);\n",
       "\n",
       "    this._init_header(this);\n",
       "    this._init_canvas(this);\n",
       "    this._init_toolbar(this);\n",
       "\n",
       "    var fig = this;\n",
       "\n",
       "    this.waiting = false;\n",
       "\n",
       "    this.ws.onopen =  function () {\n",
       "            fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
       "            fig.send_message(\"send_image_mode\", {});\n",
       "            if (mpl.ratio != 1) {\n",
       "                fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
       "            }\n",
       "            fig.send_message(\"refresh\", {});\n",
       "        }\n",
       "\n",
       "    this.imageObj.onload = function() {\n",
       "            if (fig.image_mode == 'full') {\n",
       "                // Full images could contain transparency (where diff images\n",
       "                // almost always do), so we need to clear the canvas so that\n",
       "                // there is no ghosting.\n",
       "                fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
       "            }\n",
       "            fig.context.drawImage(fig.imageObj, 0, 0);\n",
       "        };\n",
       "\n",
       "    this.imageObj.onunload = function() {\n",
       "        fig.ws.close();\n",
       "    }\n",
       "\n",
       "    this.ws.onmessage = this._make_on_message_function(this);\n",
       "\n",
       "    this.ondownload = ondownload;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_header = function() {\n",
       "    var titlebar = $(\n",
       "        '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
       "        'ui-helper-clearfix\"/>');\n",
       "    var titletext = $(\n",
       "        '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
       "        'text-align: center; padding: 3px;\"/>');\n",
       "    titlebar.append(titletext)\n",
       "    this.root.append(titlebar);\n",
       "    this.header = titletext[0];\n",
       "}\n",
       "\n",
       "\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_canvas = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var canvas_div = $('<div/>');\n",
       "\n",
       "    canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
       "\n",
       "    function canvas_keyboard_event(event) {\n",
       "        return fig.key_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    canvas_div.keydown('key_press', canvas_keyboard_event);\n",
       "    canvas_div.keyup('key_release', canvas_keyboard_event);\n",
       "    this.canvas_div = canvas_div\n",
       "    this._canvas_extra_style(canvas_div)\n",
       "    this.root.append(canvas_div);\n",
       "\n",
       "    var canvas = $('<canvas/>');\n",
       "    canvas.addClass('mpl-canvas');\n",
       "    canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
       "\n",
       "    this.canvas = canvas[0];\n",
       "    this.context = canvas[0].getContext(\"2d\");\n",
       "\n",
       "    var backingStore = this.context.backingStorePixelRatio ||\n",
       "\tthis.context.webkitBackingStorePixelRatio ||\n",
       "\tthis.context.mozBackingStorePixelRatio ||\n",
       "\tthis.context.msBackingStorePixelRatio ||\n",
       "\tthis.context.oBackingStorePixelRatio ||\n",
       "\tthis.context.backingStorePixelRatio || 1;\n",
       "\n",
       "    mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
       "\n",
       "    var rubberband = $('<canvas/>');\n",
       "    rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
       "\n",
       "    var pass_mouse_events = true;\n",
       "\n",
       "    canvas_div.resizable({\n",
       "        start: function(event, ui) {\n",
       "            pass_mouse_events = false;\n",
       "        },\n",
       "        resize: function(event, ui) {\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "        stop: function(event, ui) {\n",
       "            pass_mouse_events = true;\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "    });\n",
       "\n",
       "    function mouse_event_fn(event) {\n",
       "        if (pass_mouse_events)\n",
       "            return fig.mouse_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    rubberband.mousedown('button_press', mouse_event_fn);\n",
       "    rubberband.mouseup('button_release', mouse_event_fn);\n",
       "    // Throttle sequential mouse events to 1 every 20ms.\n",
       "    rubberband.mousemove('motion_notify', mouse_event_fn);\n",
       "\n",
       "    rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
       "    rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
       "\n",
       "    canvas_div.on(\"wheel\", function (event) {\n",
       "        event = event.originalEvent;\n",
       "        event['data'] = 'scroll'\n",
       "        if (event.deltaY < 0) {\n",
       "            event.step = 1;\n",
       "        } else {\n",
       "            event.step = -1;\n",
       "        }\n",
       "        mouse_event_fn(event);\n",
       "    });\n",
       "\n",
       "    canvas_div.append(canvas);\n",
       "    canvas_div.append(rubberband);\n",
       "\n",
       "    this.rubberband = rubberband;\n",
       "    this.rubberband_canvas = rubberband[0];\n",
       "    this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
       "    this.rubberband_context.strokeStyle = \"#000000\";\n",
       "\n",
       "    this._resize_canvas = function(width, height) {\n",
       "        // Keep the size of the canvas, canvas container, and rubber band\n",
       "        // canvas in synch.\n",
       "        canvas_div.css('width', width)\n",
       "        canvas_div.css('height', height)\n",
       "\n",
       "        canvas.attr('width', width * mpl.ratio);\n",
       "        canvas.attr('height', height * mpl.ratio);\n",
       "        canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
       "\n",
       "        rubberband.attr('width', width);\n",
       "        rubberband.attr('height', height);\n",
       "    }\n",
       "\n",
       "    // Set the figure to an initial 600x600px, this will subsequently be updated\n",
       "    // upon first draw.\n",
       "    this._resize_canvas(600, 600);\n",
       "\n",
       "    // Disable right mouse context menu.\n",
       "    $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
       "        return false;\n",
       "    });\n",
       "\n",
       "    function set_focus () {\n",
       "        canvas.focus();\n",
       "        canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    window.setTimeout(set_focus, 100);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items) {\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) {\n",
       "            // put a spacer in here.\n",
       "            continue;\n",
       "        }\n",
       "        var button = $('<button/>');\n",
       "        button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
       "                        'ui-button-icon-only');\n",
       "        button.attr('role', 'button');\n",
       "        button.attr('aria-disabled', 'false');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "\n",
       "        var icon_img = $('<span/>');\n",
       "        icon_img.addClass('ui-button-icon-primary ui-icon');\n",
       "        icon_img.addClass(image);\n",
       "        icon_img.addClass('ui-corner-all');\n",
       "\n",
       "        var tooltip_span = $('<span/>');\n",
       "        tooltip_span.addClass('ui-button-text');\n",
       "        tooltip_span.html(tooltip);\n",
       "\n",
       "        button.append(icon_img);\n",
       "        button.append(tooltip_span);\n",
       "\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    var fmt_picker_span = $('<span/>');\n",
       "\n",
       "    var fmt_picker = $('<select/>');\n",
       "    fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
       "    fmt_picker_span.append(fmt_picker);\n",
       "    nav_element.append(fmt_picker_span);\n",
       "    this.format_dropdown = fmt_picker[0];\n",
       "\n",
       "    for (var ind in mpl.extensions) {\n",
       "        var fmt = mpl.extensions[ind];\n",
       "        var option = $(\n",
       "            '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
       "        fmt_picker.append(option)\n",
       "    }\n",
       "\n",
       "    // Add hover states to the ui-buttons\n",
       "    $( \".ui-button\" ).hover(\n",
       "        function() { $(this).addClass(\"ui-state-hover\");},\n",
       "        function() { $(this).removeClass(\"ui-state-hover\");}\n",
       "    );\n",
       "\n",
       "    var status_bar = $('<span class=\"mpl-message\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
       "    // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
       "    // which will in turn request a refresh of the image.\n",
       "    this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_message = function(type, properties) {\n",
       "    properties['type'] = type;\n",
       "    properties['figure_id'] = this.id;\n",
       "    this.ws.send(JSON.stringify(properties));\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_draw_message = function() {\n",
       "    if (!this.waiting) {\n",
       "        this.waiting = true;\n",
       "        this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
       "    }\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    var format_dropdown = fig.format_dropdown;\n",
       "    var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
       "    fig.ondownload(fig, format);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
       "    var size = msg['size'];\n",
       "    if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
       "        fig._resize_canvas(size[0], size[1]);\n",
       "        fig.send_message(\"refresh\", {});\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
       "    var x0 = msg['x0'] / mpl.ratio;\n",
       "    var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
       "    var x1 = msg['x1'] / mpl.ratio;\n",
       "    var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
       "    x0 = Math.floor(x0) + 0.5;\n",
       "    y0 = Math.floor(y0) + 0.5;\n",
       "    x1 = Math.floor(x1) + 0.5;\n",
       "    y1 = Math.floor(y1) + 0.5;\n",
       "    var min_x = Math.min(x0, x1);\n",
       "    var min_y = Math.min(y0, y1);\n",
       "    var width = Math.abs(x1 - x0);\n",
       "    var height = Math.abs(y1 - y0);\n",
       "\n",
       "    fig.rubberband_context.clearRect(\n",
       "        0, 0, fig.canvas.width, fig.canvas.height);\n",
       "\n",
       "    fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
       "    // Updates the figure title.\n",
       "    fig.header.textContent = msg['label'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
       "    var cursor = msg['cursor'];\n",
       "    switch(cursor)\n",
       "    {\n",
       "    case 0:\n",
       "        cursor = 'pointer';\n",
       "        break;\n",
       "    case 1:\n",
       "        cursor = 'default';\n",
       "        break;\n",
       "    case 2:\n",
       "        cursor = 'crosshair';\n",
       "        break;\n",
       "    case 3:\n",
       "        cursor = 'move';\n",
       "        break;\n",
       "    }\n",
       "    fig.rubberband_canvas.style.cursor = cursor;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
       "    fig.message.textContent = msg['message'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
       "    // Request the server to send over a new figure.\n",
       "    fig.send_draw_message();\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
       "    fig.image_mode = msg['mode'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Called whenever the canvas gets updated.\n",
       "    this.send_message(\"ack\", {});\n",
       "}\n",
       "\n",
       "// A function to construct a web socket function for onmessage handling.\n",
       "// Called in the figure constructor.\n",
       "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
       "    return function socket_on_message(evt) {\n",
       "        if (evt.data instanceof Blob) {\n",
       "            /* FIXME: We get \"Resource interpreted as Image but\n",
       "             * transferred with MIME type text/plain:\" errors on\n",
       "             * Chrome.  But how to set the MIME type?  It doesn't seem\n",
       "             * to be part of the websocket stream */\n",
       "            evt.data.type = \"image/png\";\n",
       "\n",
       "            /* Free the memory for the previous frames */\n",
       "            if (fig.imageObj.src) {\n",
       "                (window.URL || window.webkitURL).revokeObjectURL(\n",
       "                    fig.imageObj.src);\n",
       "            }\n",
       "\n",
       "            fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
       "                evt.data);\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "        else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
       "            fig.imageObj.src = evt.data;\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        var msg = JSON.parse(evt.data);\n",
       "        var msg_type = msg['type'];\n",
       "\n",
       "        // Call the  \"handle_{type}\" callback, which takes\n",
       "        // the figure and JSON message as its only arguments.\n",
       "        try {\n",
       "            var callback = fig[\"handle_\" + msg_type];\n",
       "        } catch (e) {\n",
       "            console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        if (callback) {\n",
       "            try {\n",
       "                // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
       "                callback(fig, msg);\n",
       "            } catch (e) {\n",
       "                console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
       "            }\n",
       "        }\n",
       "    };\n",
       "}\n",
       "\n",
       "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
       "mpl.findpos = function(e) {\n",
       "    //this section is from http://www.quirksmode.org/js/events_properties.html\n",
       "    var targ;\n",
       "    if (!e)\n",
       "        e = window.event;\n",
       "    if (e.target)\n",
       "        targ = e.target;\n",
       "    else if (e.srcElement)\n",
       "        targ = e.srcElement;\n",
       "    if (targ.nodeType == 3) // defeat Safari bug\n",
       "        targ = targ.parentNode;\n",
       "\n",
       "    // jQuery normalizes the pageX and pageY\n",
       "    // pageX,Y are the mouse positions relative to the document\n",
       "    // offset() returns the position of the element relative to the document\n",
       "    var x = e.pageX - $(targ).offset().left;\n",
       "    var y = e.pageY - $(targ).offset().top;\n",
       "\n",
       "    return {\"x\": x, \"y\": y};\n",
       "};\n",
       "\n",
       "/*\n",
       " * return a copy of an object with only non-object keys\n",
       " * we need this to avoid circular references\n",
       " * http://stackoverflow.com/a/24161582/3208463\n",
       " */\n",
       "function simpleKeys (original) {\n",
       "  return Object.keys(original).reduce(function (obj, key) {\n",
       "    if (typeof original[key] !== 'object')\n",
       "        obj[key] = original[key]\n",
       "    return obj;\n",
       "  }, {});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.mouse_event = function(event, name) {\n",
       "    var canvas_pos = mpl.findpos(event)\n",
       "\n",
       "    if (name === 'button_press')\n",
       "    {\n",
       "        this.canvas.focus();\n",
       "        this.canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    var x = canvas_pos.x * mpl.ratio;\n",
       "    var y = canvas_pos.y * mpl.ratio;\n",
       "\n",
       "    this.send_message(name, {x: x, y: y, button: event.button,\n",
       "                             step: event.step,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "\n",
       "    /* This prevents the web browser from automatically changing to\n",
       "     * the text insertion cursor when the button is pressed.  We want\n",
       "     * to control all of the cursor setting manually through the\n",
       "     * 'cursor' event from matplotlib */\n",
       "    event.preventDefault();\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    // Handle any extra behaviour associated with a key event\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.key_event = function(event, name) {\n",
       "\n",
       "    // Prevent repeat events\n",
       "    if (name == 'key_press')\n",
       "    {\n",
       "        if (event.which === this._key)\n",
       "            return;\n",
       "        else\n",
       "            this._key = event.which;\n",
       "    }\n",
       "    if (name == 'key_release')\n",
       "        this._key = null;\n",
       "\n",
       "    var value = '';\n",
       "    if (event.ctrlKey && event.which != 17)\n",
       "        value += \"ctrl+\";\n",
       "    if (event.altKey && event.which != 18)\n",
       "        value += \"alt+\";\n",
       "    if (event.shiftKey && event.which != 16)\n",
       "        value += \"shift+\";\n",
       "\n",
       "    value += 'k';\n",
       "    value += event.which.toString();\n",
       "\n",
       "    this._key_event_extra(event, name);\n",
       "\n",
       "    this.send_message(name, {key: value,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
       "    if (name == 'download') {\n",
       "        this.handle_save(this, null);\n",
       "    } else {\n",
       "        this.send_message(\"toolbar_button\", {name: name});\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
       "    this.message.textContent = tooltip;\n",
       "};\n",
       "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to  previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
       "\n",
       "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
       "\n",
       "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
       "    // Create a \"websocket\"-like object which calls the given IPython comm\n",
       "    // object with the appropriate methods. Currently this is a non binary\n",
       "    // socket, so there is still some room for performance tuning.\n",
       "    var ws = {};\n",
       "\n",
       "    ws.close = function() {\n",
       "        comm.close()\n",
       "    };\n",
       "    ws.send = function(m) {\n",
       "        //console.log('sending', m);\n",
       "        comm.send(m);\n",
       "    };\n",
       "    // Register the callback with on_msg.\n",
       "    comm.on_msg(function(msg) {\n",
       "        //console.log('receiving', msg['content']['data'], msg);\n",
       "        // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
       "        ws.onmessage(msg['content']['data'])\n",
       "    });\n",
       "    return ws;\n",
       "}\n",
       "\n",
       "mpl.mpl_figure_comm = function(comm, msg) {\n",
       "    // This is the function which gets called when the mpl process\n",
       "    // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
       "\n",
       "    var id = msg.content.data.id;\n",
       "    // Get hold of the div created by the display call when the Comm\n",
       "    // socket was opened in Python.\n",
       "    var element = $(\"#\" + id);\n",
       "    var ws_proxy = comm_websocket_adapter(comm)\n",
       "\n",
       "    function ondownload(figure, format) {\n",
       "        window.open(figure.imageObj.src);\n",
       "    }\n",
       "\n",
       "    var fig = new mpl.figure(id, ws_proxy,\n",
       "                           ondownload,\n",
       "                           element.get(0));\n",
       "\n",
       "    // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
       "    // web socket which is closed, not our websocket->open comm proxy.\n",
       "    ws_proxy.onopen();\n",
       "\n",
       "    fig.parent_element = element.get(0);\n",
       "    fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
       "    if (!fig.cell_info) {\n",
       "        console.error(\"Failed to find cell for figure\", id, fig);\n",
       "        return;\n",
       "    }\n",
       "\n",
       "    var output_index = fig.cell_info[2]\n",
       "    var cell = fig.cell_info[0];\n",
       "\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
       "    var width = fig.canvas.width/mpl.ratio\n",
       "    fig.root.unbind('remove')\n",
       "\n",
       "    // Update the output cell to use the data from the current canvas.\n",
       "    fig.push_to_output();\n",
       "    var dataURL = fig.canvas.toDataURL();\n",
       "    // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
       "    // the notebook keyboard shortcuts fail.\n",
       "    IPython.keyboard_manager.enable()\n",
       "    $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
       "    fig.close_ws(fig, msg);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.close_ws = function(fig, msg){\n",
       "    fig.send_message('closing', msg);\n",
       "    // fig.ws.close()\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
       "    // Turn the data on the canvas into data in the output cell.\n",
       "    var width = this.canvas.width/mpl.ratio\n",
       "    var dataURL = this.canvas.toDataURL();\n",
       "    this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Tell IPython that the notebook contents must change.\n",
       "    IPython.notebook.set_dirty(true);\n",
       "    this.send_message(\"ack\", {});\n",
       "    var fig = this;\n",
       "    // Wait a second, then push the new image to the DOM so\n",
       "    // that it is saved nicely (might be nice to debounce this).\n",
       "    setTimeout(function () { fig.push_to_output() }, 1000);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items){\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) { continue; };\n",
       "\n",
       "        var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    // Add the status bar.\n",
       "    var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "\n",
       "    // Add the close button to the window.\n",
       "    var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
       "    var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
       "    button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
       "    button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
       "    buttongrp.append(button);\n",
       "    var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
       "    titlebar.prepend(buttongrp);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(el){\n",
       "    var fig = this\n",
       "    el.on(\"remove\", function(){\n",
       "\tfig.close_ws(fig, {});\n",
       "    });\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(el){\n",
       "    // this is important to make the div 'focusable\n",
       "    el.attr('tabindex', 0)\n",
       "    // reach out to IPython and tell the keyboard manager to turn it's self\n",
       "    // off when our div gets focus\n",
       "\n",
       "    // location in version 3\n",
       "    if (IPython.notebook.keyboard_manager) {\n",
       "        IPython.notebook.keyboard_manager.register_events(el);\n",
       "    }\n",
       "    else {\n",
       "        // location in version 2\n",
       "        IPython.keyboard_manager.register_events(el);\n",
       "    }\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    var manager = IPython.notebook.keyboard_manager;\n",
       "    if (!manager)\n",
       "        manager = IPython.keyboard_manager;\n",
       "\n",
       "    // Check for shift+enter\n",
       "    if (event.shiftKey && event.which == 13) {\n",
       "        this.canvas_div.blur();\n",
       "        event.shiftKey = false;\n",
       "        // Send a \"J\" for go to next cell\n",
       "        event.which = 74;\n",
       "        event.keyCode = 74;\n",
       "        manager.command_mode();\n",
       "        manager.handle_keydown(event);\n",
       "    }\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    fig.ondownload(fig, null);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.find_output_cell = function(html_output) {\n",
       "    // Return the cell and output element which can be found *uniquely* in the notebook.\n",
       "    // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
       "    // IPython event is triggered only after the cells have been serialised, which for\n",
       "    // our purposes (turning an active figure into a static one), is too late.\n",
       "    var cells = IPython.notebook.get_cells();\n",
       "    var ncells = cells.length;\n",
       "    for (var i=0; i<ncells; i++) {\n",
       "        var cell = cells[i];\n",
       "        if (cell.cell_type === 'code'){\n",
       "            for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
       "                var data = cell.output_area.outputs[j];\n",
       "                if (data.data) {\n",
       "                    // IPython >= 3 moved mimebundle to data attribute of output\n",
       "                    data = data.data;\n",
       "                }\n",
       "                if (data['text/html'] == html_output) {\n",
       "                    return [cell, data, j];\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    }\n",
       "}\n",
       "\n",
       "// Register the function which deals with the matplotlib target/channel.\n",
       "// The kernel may be null if the page has been refreshed.\n",
       "if (IPython.notebook.kernel != null) {\n",
       "    IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
       "}\n"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<img src=\"\" width=\"799.9999880790713\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib notebook\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib import animation\n",
    "import numpy as np\n",
    "\n",
    "font = {'family': 'roboto',\n",
    "        'color':  'darkred',\n",
    "        'weight': 'normal',\n",
    "        'size': 12,\n",
    "        }\n",
    "\n",
    "cities = []\n",
    "distances ={}\n",
    "states = []\n",
    "\n",
    "# creating plotting area\n",
    "fig = plt.figure(figsize = (8,6))\n",
    "ax = plt.axes(xlim=(60, 600), ylim=(245, 600))\n",
    "line, = ax.plot([], [], c=\"b\",linewidth = 1.5, marker = 'o', markerfacecolor = 'r', markeredgecolor = 'r',markersize = 10)\n",
    "text = ax.text(450, 565, \"\", fontdict = font)\n",
    "\n",
    "# creating initial path\n",
    "for name in romania_map.locations.keys():    \n",
    "    distances[name] = {}\n",
    "    cities.append(name)\n",
    "\n",
    "\n",
    "# distances['city1']['city2'] contains euclidean distance between their coordinates\n",
    "for name_1,coordinates_1 in romania_map.locations.items():\n",
    "    for name_2,coordinates_2 in romania_map.locations.items():\n",
    "        distances[name_1][name_2] = np.linalg.norm([coordinates_1[0] - coordinates_2[0], coordinates_1[1] - coordinates_2[1]])\n",
    "        distances[name_2][name_1] = np.linalg.norm([coordinates_1[0] - coordinates_2[0], coordinates_1[1] - coordinates_2[1]])\n",
    "\n",
    "# creating the problem        \n",
    "tsp_problem = TSP_problem(cities)\n",
    "\n",
    "# all the states as a 2-D list of paths\n",
    "states = simulated_annealing_full(tsp_problem)\n",
    "\n",
    "# calling the matplotlib animation function \n",
    "anim = animation.FuncAnimation(fig, animate, init_func = init,\n",
    "                           frames = len(states), interval = len(states), blit = True, repeat = False)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Iterative Simulated Annealing\n",
    "\n",
    "Providing the output of the previous run as input to the next run to give better performance."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "/* Put everything inside the global mpl namespace */\n",
       "window.mpl = {};\n",
       "\n",
       "\n",
       "mpl.get_websocket_type = function() {\n",
       "    if (typeof(WebSocket) !== 'undefined') {\n",
       "        return WebSocket;\n",
       "    } else if (typeof(MozWebSocket) !== 'undefined') {\n",
       "        return MozWebSocket;\n",
       "    } else {\n",
       "        alert('Your browser does not have WebSocket support.' +\n",
       "              'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
       "              'Firefox 4 and 5 are also supported but you ' +\n",
       "              'have to enable WebSockets in about:config.');\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
       "    this.id = figure_id;\n",
       "\n",
       "    this.ws = websocket;\n",
       "\n",
       "    this.supports_binary = (this.ws.binaryType != undefined);\n",
       "\n",
       "    if (!this.supports_binary) {\n",
       "        var warnings = document.getElementById(\"mpl-warnings\");\n",
       "        if (warnings) {\n",
       "            warnings.style.display = 'block';\n",
       "            warnings.textContent = (\n",
       "                \"This browser does not support binary websocket messages. \" +\n",
       "                    \"Performance may be slow.\");\n",
       "        }\n",
       "    }\n",
       "\n",
       "    this.imageObj = new Image();\n",
       "\n",
       "    this.context = undefined;\n",
       "    this.message = undefined;\n",
       "    this.canvas = undefined;\n",
       "    this.rubberband_canvas = undefined;\n",
       "    this.rubberband_context = undefined;\n",
       "    this.format_dropdown = undefined;\n",
       "\n",
       "    this.image_mode = 'full';\n",
       "\n",
       "    this.root = $('<div/>');\n",
       "    this._root_extra_style(this.root)\n",
       "    this.root.attr('style', 'display: inline-block');\n",
       "\n",
       "    $(parent_element).append(this.root);\n",
       "\n",
       "    this._init_header(this);\n",
       "    this._init_canvas(this);\n",
       "    this._init_toolbar(this);\n",
       "\n",
       "    var fig = this;\n",
       "\n",
       "    this.waiting = false;\n",
       "\n",
       "    this.ws.onopen =  function () {\n",
       "            fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
       "            fig.send_message(\"send_image_mode\", {});\n",
       "            if (mpl.ratio != 1) {\n",
       "                fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
       "            }\n",
       "            fig.send_message(\"refresh\", {});\n",
       "        }\n",
       "\n",
       "    this.imageObj.onload = function() {\n",
       "            if (fig.image_mode == 'full') {\n",
       "                // Full images could contain transparency (where diff images\n",
       "                // almost always do), so we need to clear the canvas so that\n",
       "                // there is no ghosting.\n",
       "                fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
       "            }\n",
       "            fig.context.drawImage(fig.imageObj, 0, 0);\n",
       "        };\n",
       "\n",
       "    this.imageObj.onunload = function() {\n",
       "        fig.ws.close();\n",
       "    }\n",
       "\n",
       "    this.ws.onmessage = this._make_on_message_function(this);\n",
       "\n",
       "    this.ondownload = ondownload;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_header = function() {\n",
       "    var titlebar = $(\n",
       "        '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
       "        'ui-helper-clearfix\"/>');\n",
       "    var titletext = $(\n",
       "        '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
       "        'text-align: center; padding: 3px;\"/>');\n",
       "    titlebar.append(titletext)\n",
       "    this.root.append(titlebar);\n",
       "    this.header = titletext[0];\n",
       "}\n",
       "\n",
       "\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_canvas = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var canvas_div = $('<div/>');\n",
       "\n",
       "    canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
       "\n",
       "    function canvas_keyboard_event(event) {\n",
       "        return fig.key_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    canvas_div.keydown('key_press', canvas_keyboard_event);\n",
       "    canvas_div.keyup('key_release', canvas_keyboard_event);\n",
       "    this.canvas_div = canvas_div\n",
       "    this._canvas_extra_style(canvas_div)\n",
       "    this.root.append(canvas_div);\n",
       "\n",
       "    var canvas = $('<canvas/>');\n",
       "    canvas.addClass('mpl-canvas');\n",
       "    canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
       "\n",
       "    this.canvas = canvas[0];\n",
       "    this.context = canvas[0].getContext(\"2d\");\n",
       "\n",
       "    var backingStore = this.context.backingStorePixelRatio ||\n",
       "\tthis.context.webkitBackingStorePixelRatio ||\n",
       "\tthis.context.mozBackingStorePixelRatio ||\n",
       "\tthis.context.msBackingStorePixelRatio ||\n",
       "\tthis.context.oBackingStorePixelRatio ||\n",
       "\tthis.context.backingStorePixelRatio || 1;\n",
       "\n",
       "    mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
       "\n",
       "    var rubberband = $('<canvas/>');\n",
       "    rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
       "\n",
       "    var pass_mouse_events = true;\n",
       "\n",
       "    canvas_div.resizable({\n",
       "        start: function(event, ui) {\n",
       "            pass_mouse_events = false;\n",
       "        },\n",
       "        resize: function(event, ui) {\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "        stop: function(event, ui) {\n",
       "            pass_mouse_events = true;\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "    });\n",
       "\n",
       "    function mouse_event_fn(event) {\n",
       "        if (pass_mouse_events)\n",
       "            return fig.mouse_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    rubberband.mousedown('button_press', mouse_event_fn);\n",
       "    rubberband.mouseup('button_release', mouse_event_fn);\n",
       "    // Throttle sequential mouse events to 1 every 20ms.\n",
       "    rubberband.mousemove('motion_notify', mouse_event_fn);\n",
       "\n",
       "    rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
       "    rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
       "\n",
       "    canvas_div.on(\"wheel\", function (event) {\n",
       "        event = event.originalEvent;\n",
       "        event['data'] = 'scroll'\n",
       "        if (event.deltaY < 0) {\n",
       "            event.step = 1;\n",
       "        } else {\n",
       "            event.step = -1;\n",
       "        }\n",
       "        mouse_event_fn(event);\n",
       "    });\n",
       "\n",
       "    canvas_div.append(canvas);\n",
       "    canvas_div.append(rubberband);\n",
       "\n",
       "    this.rubberband = rubberband;\n",
       "    this.rubberband_canvas = rubberband[0];\n",
       "    this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
       "    this.rubberband_context.strokeStyle = \"#000000\";\n",
       "\n",
       "    this._resize_canvas = function(width, height) {\n",
       "        // Keep the size of the canvas, canvas container, and rubber band\n",
       "        // canvas in synch.\n",
       "        canvas_div.css('width', width)\n",
       "        canvas_div.css('height', height)\n",
       "\n",
       "        canvas.attr('width', width * mpl.ratio);\n",
       "        canvas.attr('height', height * mpl.ratio);\n",
       "        canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
       "\n",
       "        rubberband.attr('width', width);\n",
       "        rubberband.attr('height', height);\n",
       "    }\n",
       "\n",
       "    // Set the figure to an initial 600x600px, this will subsequently be updated\n",
       "    // upon first draw.\n",
       "    this._resize_canvas(600, 600);\n",
       "\n",
       "    // Disable right mouse context menu.\n",
       "    $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
       "        return false;\n",
       "    });\n",
       "\n",
       "    function set_focus () {\n",
       "        canvas.focus();\n",
       "        canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    window.setTimeout(set_focus, 100);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items) {\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) {\n",
       "            // put a spacer in here.\n",
       "            continue;\n",
       "        }\n",
       "        var button = $('<button/>');\n",
       "        button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
       "                        'ui-button-icon-only');\n",
       "        button.attr('role', 'button');\n",
       "        button.attr('aria-disabled', 'false');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "\n",
       "        var icon_img = $('<span/>');\n",
       "        icon_img.addClass('ui-button-icon-primary ui-icon');\n",
       "        icon_img.addClass(image);\n",
       "        icon_img.addClass('ui-corner-all');\n",
       "\n",
       "        var tooltip_span = $('<span/>');\n",
       "        tooltip_span.addClass('ui-button-text');\n",
       "        tooltip_span.html(tooltip);\n",
       "\n",
       "        button.append(icon_img);\n",
       "        button.append(tooltip_span);\n",
       "\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    var fmt_picker_span = $('<span/>');\n",
       "\n",
       "    var fmt_picker = $('<select/>');\n",
       "    fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
       "    fmt_picker_span.append(fmt_picker);\n",
       "    nav_element.append(fmt_picker_span);\n",
       "    this.format_dropdown = fmt_picker[0];\n",
       "\n",
       "    for (var ind in mpl.extensions) {\n",
       "        var fmt = mpl.extensions[ind];\n",
       "        var option = $(\n",
       "            '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
       "        fmt_picker.append(option)\n",
       "    }\n",
       "\n",
       "    // Add hover states to the ui-buttons\n",
       "    $( \".ui-button\" ).hover(\n",
       "        function() { $(this).addClass(\"ui-state-hover\");},\n",
       "        function() { $(this).removeClass(\"ui-state-hover\");}\n",
       "    );\n",
       "\n",
       "    var status_bar = $('<span class=\"mpl-message\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
       "    // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
       "    // which will in turn request a refresh of the image.\n",
       "    this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_message = function(type, properties) {\n",
       "    properties['type'] = type;\n",
       "    properties['figure_id'] = this.id;\n",
       "    this.ws.send(JSON.stringify(properties));\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_draw_message = function() {\n",
       "    if (!this.waiting) {\n",
       "        this.waiting = true;\n",
       "        this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
       "    }\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    var format_dropdown = fig.format_dropdown;\n",
       "    var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
       "    fig.ondownload(fig, format);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
       "    var size = msg['size'];\n",
       "    if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
       "        fig._resize_canvas(size[0], size[1]);\n",
       "        fig.send_message(\"refresh\", {});\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
       "    var x0 = msg['x0'] / mpl.ratio;\n",
       "    var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
       "    var x1 = msg['x1'] / mpl.ratio;\n",
       "    var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
       "    x0 = Math.floor(x0) + 0.5;\n",
       "    y0 = Math.floor(y0) + 0.5;\n",
       "    x1 = Math.floor(x1) + 0.5;\n",
       "    y1 = Math.floor(y1) + 0.5;\n",
       "    var min_x = Math.min(x0, x1);\n",
       "    var min_y = Math.min(y0, y1);\n",
       "    var width = Math.abs(x1 - x0);\n",
       "    var height = Math.abs(y1 - y0);\n",
       "\n",
       "    fig.rubberband_context.clearRect(\n",
       "        0, 0, fig.canvas.width, fig.canvas.height);\n",
       "\n",
       "    fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
       "    // Updates the figure title.\n",
       "    fig.header.textContent = msg['label'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
       "    var cursor = msg['cursor'];\n",
       "    switch(cursor)\n",
       "    {\n",
       "    case 0:\n",
       "        cursor = 'pointer';\n",
       "        break;\n",
       "    case 1:\n",
       "        cursor = 'default';\n",
       "        break;\n",
       "    case 2:\n",
       "        cursor = 'crosshair';\n",
       "        break;\n",
       "    case 3:\n",
       "        cursor = 'move';\n",
       "        break;\n",
       "    }\n",
       "    fig.rubberband_canvas.style.cursor = cursor;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
       "    fig.message.textContent = msg['message'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
       "    // Request the server to send over a new figure.\n",
       "    fig.send_draw_message();\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
       "    fig.image_mode = msg['mode'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Called whenever the canvas gets updated.\n",
       "    this.send_message(\"ack\", {});\n",
       "}\n",
       "\n",
       "// A function to construct a web socket function for onmessage handling.\n",
       "// Called in the figure constructor.\n",
       "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
       "    return function socket_on_message(evt) {\n",
       "        if (evt.data instanceof Blob) {\n",
       "            /* FIXME: We get \"Resource interpreted as Image but\n",
       "             * transferred with MIME type text/plain:\" errors on\n",
       "             * Chrome.  But how to set the MIME type?  It doesn't seem\n",
       "             * to be part of the websocket stream */\n",
       "            evt.data.type = \"image/png\";\n",
       "\n",
       "            /* Free the memory for the previous frames */\n",
       "            if (fig.imageObj.src) {\n",
       "                (window.URL || window.webkitURL).revokeObjectURL(\n",
       "                    fig.imageObj.src);\n",
       "            }\n",
       "\n",
       "            fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
       "                evt.data);\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "        else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
       "            fig.imageObj.src = evt.data;\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        var msg = JSON.parse(evt.data);\n",
       "        var msg_type = msg['type'];\n",
       "\n",
       "        // Call the  \"handle_{type}\" callback, which takes\n",
       "        // the figure and JSON message as its only arguments.\n",
       "        try {\n",
       "            var callback = fig[\"handle_\" + msg_type];\n",
       "        } catch (e) {\n",
       "            console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        if (callback) {\n",
       "            try {\n",
       "                // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
       "                callback(fig, msg);\n",
       "            } catch (e) {\n",
       "                console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
       "            }\n",
       "        }\n",
       "    };\n",
       "}\n",
       "\n",
       "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
       "mpl.findpos = function(e) {\n",
       "    //this section is from http://www.quirksmode.org/js/events_properties.html\n",
       "    var targ;\n",
       "    if (!e)\n",
       "        e = window.event;\n",
       "    if (e.target)\n",
       "        targ = e.target;\n",
       "    else if (e.srcElement)\n",
       "        targ = e.srcElement;\n",
       "    if (targ.nodeType == 3) // defeat Safari bug\n",
       "        targ = targ.parentNode;\n",
       "\n",
       "    // jQuery normalizes the pageX and pageY\n",
       "    // pageX,Y are the mouse positions relative to the document\n",
       "    // offset() returns the position of the element relative to the document\n",
       "    var x = e.pageX - $(targ).offset().left;\n",
       "    var y = e.pageY - $(targ).offset().top;\n",
       "\n",
       "    return {\"x\": x, \"y\": y};\n",
       "};\n",
       "\n",
       "/*\n",
       " * return a copy of an object with only non-object keys\n",
       " * we need this to avoid circular references\n",
       " * http://stackoverflow.com/a/24161582/3208463\n",
       " */\n",
       "function simpleKeys (original) {\n",
       "  return Object.keys(original).reduce(function (obj, key) {\n",
       "    if (typeof original[key] !== 'object')\n",
       "        obj[key] = original[key]\n",
       "    return obj;\n",
       "  }, {});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.mouse_event = function(event, name) {\n",
       "    var canvas_pos = mpl.findpos(event)\n",
       "\n",
       "    if (name === 'button_press')\n",
       "    {\n",
       "        this.canvas.focus();\n",
       "        this.canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    var x = canvas_pos.x * mpl.ratio;\n",
       "    var y = canvas_pos.y * mpl.ratio;\n",
       "\n",
       "    this.send_message(name, {x: x, y: y, button: event.button,\n",
       "                             step: event.step,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "\n",
       "    /* This prevents the web browser from automatically changing to\n",
       "     * the text insertion cursor when the button is pressed.  We want\n",
       "     * to control all of the cursor setting manually through the\n",
       "     * 'cursor' event from matplotlib */\n",
       "    event.preventDefault();\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    // Handle any extra behaviour associated with a key event\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.key_event = function(event, name) {\n",
       "\n",
       "    // Prevent repeat events\n",
       "    if (name == 'key_press')\n",
       "    {\n",
       "        if (event.which === this._key)\n",
       "            return;\n",
       "        else\n",
       "            this._key = event.which;\n",
       "    }\n",
       "    if (name == 'key_release')\n",
       "        this._key = null;\n",
       "\n",
       "    var value = '';\n",
       "    if (event.ctrlKey && event.which != 17)\n",
       "        value += \"ctrl+\";\n",
       "    if (event.altKey && event.which != 18)\n",
       "        value += \"alt+\";\n",
       "    if (event.shiftKey && event.which != 16)\n",
       "        value += \"shift+\";\n",
       "\n",
       "    value += 'k';\n",
       "    value += event.which.toString();\n",
       "\n",
       "    this._key_event_extra(event, name);\n",
       "\n",
       "    this.send_message(name, {key: value,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
       "    if (name == 'download') {\n",
       "        this.handle_save(this, null);\n",
       "    } else {\n",
       "        this.send_message(\"toolbar_button\", {name: name});\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
       "    this.message.textContent = tooltip;\n",
       "};\n",
       "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to  previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
       "\n",
       "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
       "\n",
       "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
       "    // Create a \"websocket\"-like object which calls the given IPython comm\n",
       "    // object with the appropriate methods. Currently this is a non binary\n",
       "    // socket, so there is still some room for performance tuning.\n",
       "    var ws = {};\n",
       "\n",
       "    ws.close = function() {\n",
       "        comm.close()\n",
       "    };\n",
       "    ws.send = function(m) {\n",
       "        //console.log('sending', m);\n",
       "        comm.send(m);\n",
       "    };\n",
       "    // Register the callback with on_msg.\n",
       "    comm.on_msg(function(msg) {\n",
       "        //console.log('receiving', msg['content']['data'], msg);\n",
       "        // Pass the mpl event to the overridden (by mpl) onmessage function.\n",
       "        ws.onmessage(msg['content']['data'])\n",
       "    });\n",
       "    return ws;\n",
       "}\n",
       "\n",
       "mpl.mpl_figure_comm = function(comm, msg) {\n",
       "    // This is the function which gets called when the mpl process\n",
       "    // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
       "\n",
       "    var id = msg.content.data.id;\n",
       "    // Get hold of the div created by the display call when the Comm\n",
       "    // socket was opened in Python.\n",
       "    var element = $(\"#\" + id);\n",
       "    var ws_proxy = comm_websocket_adapter(comm)\n",
       "\n",
       "    function ondownload(figure, format) {\n",
       "        window.open(figure.imageObj.src);\n",
       "    }\n",
       "\n",
       "    var fig = new mpl.figure(id, ws_proxy,\n",
       "                           ondownload,\n",
       "                           element.get(0));\n",
       "\n",
       "    // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
       "    // web socket which is closed, not our websocket->open comm proxy.\n",
       "    ws_proxy.onopen();\n",
       "\n",
       "    fig.parent_element = element.get(0);\n",
       "    fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
       "    if (!fig.cell_info) {\n",
       "        console.error(\"Failed to find cell for figure\", id, fig);\n",
       "        return;\n",
       "    }\n",
       "\n",
       "    var output_index = fig.cell_info[2]\n",
       "    var cell = fig.cell_info[0];\n",
       "\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
       "    var width = fig.canvas.width/mpl.ratio\n",
       "    fig.root.unbind('remove')\n",
       "\n",
       "    // Update the output cell to use the data from the current canvas.\n",
       "    fig.push_to_output();\n",
       "    var dataURL = fig.canvas.toDataURL();\n",
       "    // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
       "    // the notebook keyboard shortcuts fail.\n",
       "    IPython.keyboard_manager.enable()\n",
       "    $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
       "    fig.close_ws(fig, msg);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.close_ws = function(fig, msg){\n",
       "    fig.send_message('closing', msg);\n",
       "    // fig.ws.close()\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
       "    // Turn the data on the canvas into data in the output cell.\n",
       "    var width = this.canvas.width/mpl.ratio\n",
       "    var dataURL = this.canvas.toDataURL();\n",
       "    this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Tell IPython that the notebook contents must change.\n",
       "    IPython.notebook.set_dirty(true);\n",
       "    this.send_message(\"ack\", {});\n",
       "    var fig = this;\n",
       "    // Wait a second, then push the new image to the DOM so\n",
       "    // that it is saved nicely (might be nice to debounce this).\n",
       "    setTimeout(function () { fig.push_to_output() }, 1000);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items){\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) { continue; };\n",
       "\n",
       "        var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    // Add the status bar.\n",
       "    var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "\n",
       "    // Add the close button to the window.\n",
       "    var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
       "    var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
       "    button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
       "    button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
       "    buttongrp.append(button);\n",
       "    var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
       "    titlebar.prepend(buttongrp);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(el){\n",
       "    var fig = this\n",
       "    el.on(\"remove\", function(){\n",
       "\tfig.close_ws(fig, {});\n",
       "    });\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(el){\n",
       "    // this is important to make the div 'focusable\n",
       "    el.attr('tabindex', 0)\n",
       "    // reach out to IPython and tell the keyboard manager to turn it's self\n",
       "    // off when our div gets focus\n",
       "\n",
       "    // location in version 3\n",
       "    if (IPython.notebook.keyboard_manager) {\n",
       "        IPython.notebook.keyboard_manager.register_events(el);\n",
       "    }\n",
       "    else {\n",
       "        // location in version 2\n",
       "        IPython.keyboard_manager.register_events(el);\n",
       "    }\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    var manager = IPython.notebook.keyboard_manager;\n",
       "    if (!manager)\n",
       "        manager = IPython.keyboard_manager;\n",
       "\n",
       "    // Check for shift+enter\n",
       "    if (event.shiftKey && event.which == 13) {\n",
       "        this.canvas_div.blur();\n",
       "        event.shiftKey = false;\n",
       "        // Send a \"J\" for go to next cell\n",
       "        event.which = 74;\n",
       "        event.keyCode = 74;\n",
       "        manager.command_mode();\n",
       "        manager.handle_keydown(event);\n",
       "    }\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    fig.ondownload(fig, null);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.find_output_cell = function(html_output) {\n",
       "    // Return the cell and output element which can be found *uniquely* in the notebook.\n",
       "    // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
       "    // IPython event is triggered only after the cells have been serialised, which for\n",
       "    // our purposes (turning an active figure into a static one), is too late.\n",
       "    var cells = IPython.notebook.get_cells();\n",
       "    var ncells = cells.length;\n",
       "    for (var i=0; i<ncells; i++) {\n",
       "        var cell = cells[i];\n",
       "        if (cell.cell_type === 'code'){\n",
       "            for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
       "                var data = cell.output_area.outputs[j];\n",
       "                if (data.data) {\n",
       "                    // IPython >= 3 moved mimebundle to data attribute of output\n",
       "                    data = data.data;\n",
       "                }\n",
       "                if (data['text/html'] == html_output) {\n",
       "                    return [cell, data, j];\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    }\n",
       "}\n",
       "\n",
       "// Register the function which deals with the matplotlib target/channel.\n",
       "// The kernel may be null if the page has been refreshed.\n",
       "if (IPython.notebook.kernel != null) {\n",
       "    IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
       "}\n"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<img src=\"\" width=\"799.9999880790713\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "next_state = cities\n",
    "states = []\n",
    "\n",
    "# creating plotting area\n",
    "fig = plt.figure(figsize = (8,6))\n",
    "ax = plt.axes(xlim=(60, 600), ylim=(245, 600))\n",
    "line, = ax.plot([], [], c=\"b\",linewidth = 1.5, marker = 'o', markerfacecolor = 'r', markeredgecolor = 'r',markersize = 10)\n",
    "text = ax.text(450, 565, \"\", fontdict = font)\n",
    "\n",
    "# to plot only the final states of every simulated annealing iteration\n",
    "for iterations in range(100):\n",
    "    tsp_problem = TSP_problem(next_state)  \n",
    "    states.append(simulated_annealing(tsp_problem))\n",
    "    next_state = states[-1]\n",
    "    \n",
    "anim = animation.FuncAnimation(fig, animate, init_func=init,\n",
    "                           frames=len(states),interval=len(states), blit=True, repeat = False)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  },
  "widgets": {
   "state": {},
   "version": "1.1.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
